package demo.android.myapplication.demos.views

import android.content.Context
import android.graphics.*
import android.util.AttributeSet
import android.view.View
import cn.android.basic.centerX
import cn.android.basic.dp

/**
 * <br>createBy Gw
 * <br>createTime: 2021/7/7 15:41
 * 「荆棘」三角形 CornerPathEffect and DiscretePathEffect
 */
class TriangleView @JvmOverloads constructor(
    context: Context, attrs: AttributeSet? = null, defStyleAttr: Int = 0
) : View(context, attrs, defStyleAttr) {

    private val paint = Paint(Paint.ANTI_ALIAS_FLAG)
    private val path = Path()
    private val padding = 10.dp

    init {
        paint.style = Paint.Style.STROKE
        paint.strokeWidth = 3.dp
//        paint.pathEffect = CornerPathEffect(5.dp)
//        paint.pathEffect = DiscretePathEffect(10f, 5.dp)
        paint.pathEffect = SumPathEffect(CornerPathEffect(5.dp), DiscretePathEffect(10f, 5.dp))
    }

    override fun onSizeChanged(w: Int, h: Int, oldw: Int, oldh: Int) {
        super.onSizeChanged(w, h, oldw, oldh)
        path.reset()
        path.moveTo(centerX, padding)
        path.lineTo(width.toFloat() - padding, height.toFloat() - padding)
        path.lineTo(padding, height.toFloat() - padding)
        path.close()
    }

    override fun onDraw(canvas: Canvas) {
        super.onDraw(canvas)
        canvas.drawPath(path, paint)
    }
}